package com.xuebank.nfe.nfecar.beas

import android.support.v4.app.Fragment
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.xuebank.nfe.nfecar.beas.titlebar.NewTitleBar
import com.wzn.libaray.utils.Logger
import com.xuebank.nfe.nfecar.R

/**
 * Created by Wind_Fantasy on 15/4/2.
 */
abstract class BaseTitleBarActivity : BaseActivity() {

    var titleBar: NewTitleBar? = null
        get() {
            if (field == null) {
                field = NewTitleBar(this)
            }
            return field
        }
    lateinit protected var mContentRootView: FrameLayout

    override fun onContentChanged() {
        super.onContentChanged()
        initTitleBar()
    }

    override fun setContentView(layoutResID: Int) {
        super.setContentView(makeFinalContentView(layoutResID))
    }

    override fun setContentView(view: View) {
        super.setContentView(makeFinalContentView(view))
    }

    protected fun makeFinalContentView(layoutResID: Int): View {
        return makeFinalContentView(layoutInflater.inflate(layoutResID, null, false))
    }

    protected fun makeFinalContentView(contentView: View): View {
        if (null == contentView.background) {
            contentView.setBackgroundResource(R.color.main_background)
        }
        val linearLayout = LinearLayout(this)
        linearLayout.fitsSystemWindows = false
        linearLayout.orientation = LinearLayout.VERTICAL

        initTitleBarStyle()
        linearLayout.addView(titleBar)

        val contentRootView = FrameLayout(this)
        val contentRootViewLayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        linearLayout.addView(contentRootView, contentRootViewLayoutParams)
        mContentRootView = contentRootView

        var contentViewLayoutParams: ViewGroup.LayoutParams? = contentView.layoutParams
        if (null == contentViewLayoutParams)
            contentViewLayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        contentRootView.addView(contentView, contentViewLayoutParams)

        val layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT)
        linearLayout.layoutParams = layoutParams

        return linearLayout
    }

    override fun onAttachFragment(fragment: Fragment?) {
        super.onAttachFragment(fragment)
        Logger.e(TAG, "onAttachFragment")
    }

    open fun initTitleBar(){
//        StatusBarUtil.setTranslucent(this)
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//        }

    }

    override fun getContentRootView(): FrameLayout? {
        return mContentRootView
    }
}
